package com.example.mybank.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybank.entity.Buycount;
import com.example.mybank.mapper.BuycountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/buycount")
public class BuycountController {

    @Autowired
    BuycountMapper buycountMapper;

    // 管理员：获取所有记录
    @GetMapping("/all")
    public List<Buycount> getAll(){
        return buycountMapper.selectList(null);
    }

    // 查看指定用户的记录
    @GetMapping("/users/{userID}")
    public List<Buycount> getAllByUsers(@PathVariable("userID")Long userID){
        QueryWrapper<Buycount> queryWrapper = new QueryWrapper();
        queryWrapper.eq("users",userID);
        return buycountMapper.selectList(queryWrapper);
    }


    // 查看指定产品的记录
    @GetMapping("/product/{productID}")
    public List<Buycount> getAllByProduct(@PathVariable("productID")Long productID){
        QueryWrapper<Buycount> queryWrapper = new QueryWrapper();
        queryWrapper.eq("product",productID);
        return buycountMapper.selectList(queryWrapper);
    }

    // 查看指定产品的记录
    @GetMapping("/mix/{userID}/{productID}")
    public List<Buycount> getAllByProduct(@PathVariable("userID")Long userID,
                                          @PathVariable("productID")Long productID){
        QueryWrapper<Buycount> queryWrapper = new QueryWrapper();
        queryWrapper.eq("users",userID);
        queryWrapper.eq("product",productID);
        return buycountMapper.selectList(queryWrapper);
    }


}
